package com.ramotion.expandingcollection;

import java.util.List;
import java.util.Optional;

import ohos.aafwk.ability.Ability;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;


/**
 * SampleItemProvider
 *
 * @since 2021-04-25
 */
public class CardListItemProvider extends BaseItemProvider {
    private Ability ability;
    private CardDataImpl data;
    private List<CardList> list;
    private float alpha = 1;

    /**
     * onStart
     *
     * @param ability 上下文
     * @param data    数据
     * @param list    数据
     */
    public CardListItemProvider(Ability ability, CardDataImpl data, List<CardList> list) {
        this.ability = ability;
        this.data = data;
        this.list = list;
    }

    /**
     * getCount
     *
     * @return count
     */
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    /**
     * setAlpha
     *
     * @param alpha 透明度
     */
    public void setAlpha(float alpha) {
        this.alpha = alpha;
    }

    /**
     * getCount
     *
     * @param pos 位置
     * @return obj
     */
    @Override
    public Object getItem(int pos) {
        if (list != null && pos >= 0 && pos < list.size()) {
            return list.get(pos);
        }
        return Optional.empty();
    }

    /**
     * setIsVertical
     *
     * @param pos 位置
     * @return pos
     */
    @Override
    public long getItemId(int pos) {
        return pos;
    }

    /**
     * setIsVertical
     *
     * @param pos
     * @param component
     * @param componentContainer
     * @return cpt
     */
    @Override
    public Component getComponent(int pos, Component component, ComponentContainer componentContainer) {
        Component cpt;
        if (pos == 0) {
            cpt = LayoutScatter.getInstance(ability).parse(ResourceTable.Layout_layout_header, null, false);
            Image image = (Image) cpt.findComponentById(ResourceTable.Id_image);
            Text text = (Text) cpt.findComponentById(ResourceTable.Id_text);
            image.setPixelMap(data.getHeadBackgroundResource());
            text.setText(data.getCardTitle());
            image.setAlpha(alpha);
        } else {
            cpt = LayoutScatter.getInstance(ability).parse(ResourceTable.Layout_list_item, null, false);
            Text text = (Text) cpt.findComponentById(ResourceTable.Id_list_item_text);
            String itemData = list.get(pos - 1).getName();
            text.setText(itemData);
            if (list.get(pos - 1).isSelected()) {
                text.setBackground(ResUtils.getShapeElement(ability, ResourceTable.Color_colorAccent));
                text.setText("Tap again to remove!");
            }
            text.setClickedListener(component1 -> {
                String tapToRemoveText = "Tap again to remove!";
                Text view = (Text) component1;
                if (view.getText().equals(tapToRemoveText)) {
                    component1.setBackground(ResUtils.getShapeElement(ability, ResourceTable.Color_colorPrimary));
                    list.remove(pos - 1);
                    list.get(pos - 1).setSelected(false);
                    notifyDataChanged();
                } else {
                    view.setText(tapToRemoveText);
                    component1.setBackground(ResUtils.getShapeElement(ability, ResourceTable.Color_colorAccent));
                    list.get(pos - 1).setSelected(true);
                }
            });
        }
        cpt.setClipEnabled(false);
        return cpt;
    }
}
